<template>
  <Container>
    <template v-slot:searchForm>
      <SearchForm :formItemList="formItemList" @search="search"></SearchForm>
    </template>
    <template v-slot:btnList>
      <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
    </template>
    <template v-slot:content v-loading="loading">
      <div class="tableModule">
        <TableData :tableThead="tableThead" :config="table_config" :dataList="dataList" :loading="loading" :page="page"
          :size="size" :total="total" @selection-change="selectionChange" @handleCurrentChange="handleCurrentChange"
          @handleSizeChange="handleSizeChange">
        </TableData>
      </div>
    </template>
    <template v-slot:footer></template>
    <template v-slot:dialogModule>
      <AddEdit :title="'区域'" @closeDialog="closeDialog" :addEditFlag="addEditFlag" :dialogVisible="dialogVisible"
        :data="formData">
      </AddEdit>
    </template>
  </Container>
</template>

<script>
import Container from "@/components/Container"
import AddEdit from './components/addEdit.vue';
import areaApi from '@/api/area'
export default {
  name: 'EnergyOverview',
  components: { AddEdit ,Container},
  data() {
    return {
      dialogVisible: false,
      addEditFlag: 'add',
      btnList: ['add', 'delete'],
      size: 10,
      page: 1,
      total: 10,
      dataList: [],
      formItemList: [
        {
          label: '区域名称',
          type: 'input',
          param: 'areaName',
          defaultSelect: '',
          multiple: false,
          isShow: true
        }
      ],
      tableThead: [
        { label: '区域名称', prop: 'areaName' }
      ],
      // 配置项
      table_config: {
        isCheckBox: true,
        isOperation: true,
        operation: {
          label: '操作',
          width: '200',
          cols: [
            {
              label: '编辑',
              type: 'primary',
              flag: 'edit',
              handleRow: this.handleRow
            },
            {
              label: '删除',
              type: 'danger',
              flag: 'delete',
              handleRow: this.handleRow
            }
          ]
        },
      },
      selectList: [],
      searchFormData: {},
      formData: {}
    }
  },
  mounted() {
    this.queryPageByCondition();
  },
  methods: {
    // 多选方法
    selectionChange(val) {
      this.selectList = val || []
    },
    //获取选中Btn
    getBtnEvent(flag) {
      if (flag == 'add') {
        this.dialogVisible = true
      } else if (flag == 'delete') {
        if (this.selectList.length > 0) {
          this.delList(this.selectList)
        } else {
          this.$message.warning('请先选择表单数据项后再删除！')
        }
      }
      this.addEditFlag = flag
    },
    delList(deleteList = [], key = 'areaId') {
      let list = []
      deleteList.forEach(item => {
        list.push(item[key])
      })
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await areaApi.delList(list);
        if (res.code == 1) {
          this.$message.success(res.msg || '删除成功');
          this.queryPageByCondition();
        } else {
          this.$message.warning(res.msg || '删除失败');
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 操作方法
    handleRow(rowData, item) {
      if (item.flag == 'edit') {
        this.dialogVisible = true;
        this.formData = JSON.parse(JSON.stringify(rowData))
      } else if (item.flag == 'delete') {
        this.delList([rowData]);
      }
      this.addEditFlag = item.flag
    },
    //查询表单数据
    queryPageByCondition() {
      this.loading = true;
      areaApi.queryPageByCondition({
        page: this.page,
        size: this.size,
        entity: {
          ...this.searchFormData,
          organizationNo:sessionStorage.organizationNo
        }
      }).then(res => {
        if (res.code == 1) {
          let { content, totalElements,totalPages } = res.data || {}
          this.dataList = content || [];
          this.total = totalElements || 10;
        } else {
          this.dataList = [];
        }
        this.loading = false;
      }).catch(err => {
        this.loading = false;
      })
    },
    search(data) {
      this.page = 1;
      this.searchFormData = { ...this.searchFormData, ...data };
      this.queryPageByCondition();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryPageByCondition();
    },
    handleSizeChange(size) {
      this.size = size;
      this.queryPageByCondition();
    },
    //关闭弹窗
    closeDialog() {
      this.dialogVisible = false;
      this.queryPageByCondition();
    },
  }
}
</script>

<style lang="scss" scoped></style>
